#include "desktopnote.h"
#include "ui_desktopnote.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
#include "xlsxchartsheet.h"
#include "xlsxdocument.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"

#include <QDebug>

DesktopNote::DesktopNote(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DesktopNote)
{
    ui->setupUi(this);
    ui->NoteAddPushButton->setIcon(QIcon(":/Icon/add.ico"));
    ui->NoteAddPushButton->setIconSize(QSize(32,32));

    //隐藏滚动条
    ui->NoteItemListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->NoteItemListWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //去掉窗口边框
    this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());

    //鼠标右键弹出Menu
    MouseRightButtonMenu = new QMenu(this);        
    QAction *action1 = MouseRightButtonMenu->addAction("关闭");
    action1->setIconVisibleInMenu(false);//关闭QAction的Icon
    QAction *action2 = MouseRightButtonMenu->addAction("缩小至托盘");
    action2->setIconVisibleInMenu(false);
    connect(action1,SIGNAL(triggered(bool)),this,SLOT(MouseRightButtonCloseSlot()));
    connect(action2,SIGNAL(triggered(bool)),this,SLOT(MouseRightButtonHideSlot()));

    //创建托盘
    mSysTrayIcon = new QSystemTrayIcon(this);
    mSysTrayIcon->setIcon(QIcon(":/Icon/logo_64.ico"));
    connect(mSysTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(show()));
    createActions();
    createMenu();
    mSysTrayIcon->show();

    RunGetBianQianMessage();
}

DesktopNote::~DesktopNote()
{
    delete ui;
}

void DesktopNote::MouseRightButtonCloseSlot()
{
    this->close();
}

void DesktopNote::MouseRightButtonHideSlot()
{
    this->hide();
}

//创建托盘菜单
void DesktopNote::createActions()
{
    mShowMainAction = new QAction("展示", this);
    mShowMainAction->setIconVisibleInMenu(false);
    connect(mShowMainAction,&QAction::triggered,[&](){this->show();});

    mHideAppAction = new QAction("隐藏", this);
    mHideAppAction->setIconVisibleInMenu(false);
    connect(mHideAppAction,&QAction::triggered,[&](){this->hide();});

    mExitAppAction = new QAction("退出", this);
    mExitAppAction->setIconVisibleInMenu(false);
    connect(mExitAppAction,&QAction::triggered, [&](){this->close();});
}

void DesktopNote::createMenu()
{
    QMenu *mMenu = new QMenu(this);
    mMenu->addAction(mShowMainAction);
    mMenu->addAction(mHideAppAction);
    mMenu->addAction(mExitAppAction);
    mSysTrayIcon->setContextMenu(mMenu);
}

//运行读取便签信息
void DesktopNote::RunGetBianQianMessage(void)
{
    QXlsx::Document xlsx("Data.xlsx");

    if (xlsx.dimension().lastRow() == 1)
    {
        return;
    }

    //循环读取至最后一行
    for (int Itera = 1; Itera <= xlsx.dimension().lastRow(); Itera++)
    {
        QString Month       = xlsx.read(Itera,1).toString();
        QString Day         = xlsx.read(Itera,2).toString();
        QString Week        = xlsx.read(Itera,3).toString();
        QString MettingTime = xlsx.read(Itera,4).toString(); //会议时间
        QString MettingAddr = xlsx.read(Itera,5).toString(); //会议地点
        QString EventContent = xlsx.read(Itera,6).toString();//事件内容

        if (Month == "" && Day == "" && Week == "" && MettingAddr == "" && MettingTime == "" && EventContent == "")
        {
            break;
        }

        RunGetBianQianMessageSetItem(Month,Day,Week,MettingTime,MettingAddr,EventContent);

        qDebug()<<Month<<","<<Day<<","<<Week<<","<<MettingTime<<","<<MettingAddr<<","<<EventContent;
    }
    xlsx.saveAs("Data.xlsx");
}

//将读取出的信息创建为Item添加到ListWidget
void DesktopNote::RunGetBianQianMessageSetItem(QString Month,QString Day, QString Week, QString MettingTime, QString MettingAddr, QString Message)
{
    QListWidgetItem *pItem = new QListWidgetItem("");
    pItem->setSizeHint(QSize(381,34));
    ui->NoteItemListWidget->addItem(pItem);

    NoteItem *NI = new NoteItem(pItem,Month,Day,Week,MettingTime,MettingAddr,Message);

    ui->NoteItemListWidget->setItemWidget(pItem, NI);
    ui->NoteItemListWidget->setCurrentItem(pItem);

    //绑定闹钟按钮槽
    connect(NI, SIGNAL(AlarmSettingPushSignals(QListWidgetItem *)),this,SLOT(AlarmSettingPushSlot(QListWidgetItem *)));
    //绑定闹钟
    connect(NI, SIGNAL(AlarmSettingEnterSignals(int,int,int,QString,QString,QString)),this,SLOT(AlarmSettingEnterSlot(int,int,int,QString,QString,QString)));
    connect(NI, SIGNAL(NoteItemDelSignal(QListWidgetItem*)),this,SLOT(ListWidgetItemDelSlot(QListWidgetItem*)));
}

// 鼠标按下事件
void DesktopNote::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_bDrag1 = true;
        //获取数据初始位置
        mouseStartPoint = event->globalPos();
        //获取窗口初始位置
        windowTopLeftPoint = this->frameGeometry().topLeft();
        //触发一次paintEvent
        QWidget::update();
    }

    if (event->button() == Qt::RightButton)
    {
        MouseRightButtonMenu->exec(event->globalPos());
    }
}

// 鼠标移动事件
void DesktopNote::mouseMoveEvent(QMouseEvent *event)
{
    if (m_bDrag1)
    {
        //获得鼠标移动距离
        QPoint distance = event->globalPos() - mouseStartPoint;
        //改变窗口位置
        this->move(windowTopLeftPoint + distance);
    }
}

// 鼠标释放事件
void DesktopNote::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_bDrag1 = false;
        QWidget::update();
    }
}


//移动行
void MoveRow(QXlsx::Document &xlsx, int sourceRowIndex, int targetRowIndex)
{
    QString Month = xlsx.read(sourceRowIndex,1).toString();
    QString Day   = xlsx.read(sourceRowIndex,2).toString();
    QString Week  = xlsx.read(sourceRowIndex,3).toString();
    QString MettingTime = xlsx.read(sourceRowIndex,4).toString(); //会议时间
    QString MettingAddr = xlsx.read(sourceRowIndex,5).toString(); //会议地点
    QString EventContent = xlsx.read(sourceRowIndex,6).toString();//事件内容

    xlsx.write(targetRowIndex,1,Month);
    xlsx.write(targetRowIndex,2,Day);
    xlsx.write(targetRowIndex,3,Week);
    xlsx.write(targetRowIndex,4,MettingTime);
    xlsx.write(targetRowIndex,5,MettingAddr);
    xlsx.write(targetRowIndex,6,EventContent);
}

//覆盖excel表格内容(获取到删除的那条索引号，在EXCEL中同步删除覆盖)
void DesktopNote::QXlsxDataReplace(void)
{
    QXlsx::Document xlsx("Data.xlsx");
    for (int Itera = CurrDelItemIndex + 1; Itera < xlsx.dimension().lastRow() + 2; Itera++)
    {
       MoveRow(xlsx, Itera, Itera - 1);
    }

    xlsx.saveAs("Data.xlsx");
}

//垃圾桶按键槽函数
void DesktopNote::ListWidgetItemDelSlot(QListWidgetItem *pItem)
{
    ui->NoteItemListWidget->clearFocus();
    ui->NoteItemListWidget->clearSelection();
    ui->NoteItemListWidget->setCurrentItem(pItem);

    QMessageBox::StandardButton btn = QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("是否删除？"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if(QMessageBox::No == btn)
    {
        return;
    }

    if (ui->NoteItemListWidget->currentItem()->isSelected())
    {
        //获取当前Item索引
        CurrDelItemIndex = ui->NoteItemListWidget->currentIndex().row() + 1;
        QXlsxDataReplace();
        qDebug()<<"CurrDelItemIndex: "<<CurrDelItemIndex;
    }
    else
    {
        qDebug()<<"CurrDelItemIndex not selected: "<<CurrDelItemIndex;
    }

    ui->NoteItemListWidget->removeItemWidget(pItem);        
    delete pItem;
    pItem = nullptr;
}

//Item添加按钮槽函数
void DesktopNote::on_NoteAddPushButton_clicked()
{
    QListWidgetItem *pItem = new QListWidgetItem("");
    pItem->setSizeHint(QSize(381,34));
    ui->NoteItemListWidget->addItem(pItem);

    NoteItem *NI = new NoteItem(pItem);

    ui->NoteItemListWidget->setItemWidget(pItem, NI);
    ui->NoteItemListWidget->setCurrentItem(pItem);

    //绑定闹钟按钮槽
    connect(NI, SIGNAL(AlarmSettingPushSignals(QListWidgetItem *)),this,SLOT(AlarmSettingPushSlot(QListWidgetItem *)));
    //绑定闹钟
    connect(NI, SIGNAL(AlarmSettingEnterSignals(int,int,int,QString,QString,QString)),this,SLOT(AlarmSettingEnterSlot(int,int,int,QString,QString,QString)));
    connect(NI, SIGNAL(NoteItemDelSignal(QListWidgetItem*)),this,SLOT(ListWidgetItemDelSlot(QListWidgetItem*)));
}

//在点击闹钟按钮的时候获取本行的索引
void DesktopNote::AlarmSettingPushSlot(QListWidgetItem *pItem)
{
    ui->NoteItemListWidget->clearFocus();
    ui->NoteItemListWidget->clearSelection();
    ui->NoteItemListWidget->setCurrentItem(pItem);

    CurrItemIndex = ui->NoteItemListWidget->currentRow() + 1;
    qDebug()<<"CurrDelItemIndex: "<<CurrDelItemIndex;
}

//在点击保存按钮后,将数据保存到excel中；
void DesktopNote::AlarmSettingEnterSlot(int A,int B,int C,QString D,QString E,QString F)
{
    QXlsx::Document xlsx("Data.xlsx");
    xlsx.write(CurrItemIndex,1,QString::number(A));
    xlsx.write(CurrItemIndex,2,QString::number(B));
    xlsx.write(CurrItemIndex,3,QString::number(C));
    xlsx.write(CurrItemIndex,4,D);
    xlsx.write(CurrItemIndex,5,E);
    xlsx.write(CurrItemIndex,6,F);
    xlsx.saveAs("Data.xlsx");
}
